package com.aidex.web.controller.biz;

import com.aidex.biz.domain.ShoppingCar;
import com.aidex.biz.service.ShoppingCarService;
import com.aidex.common.annotation.Log;
import com.aidex.common.core.controller.BaseController;
import com.aidex.common.core.domain.R;
import com.aidex.common.core.page.PageDomain;
import com.aidex.common.enums.BusinessType;
import com.aidex.common.utils.SecurityUtils;
import com.aidex.common.utils.poi.ExcelUtil;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 购物车Controller
 * @author sean
 * @email 2074844820@qq.com
 * @date 2023-07-02
 */
@RestController
@RequestMapping("/biz/bizShoppingCar")
public class ShoppingCarController extends BaseController
{
    @Autowired
    private ShoppingCarService shoppingCarService;

    /**
     * 查询购物车列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:list')")
    @GetMapping("/list")
    public R<PageInfo> list(ShoppingCar shoppingCar, HttpServletRequest request, HttpServletResponse response)
    {
        if(!SecurityUtils.isAdmin(SecurityUtils.getUserId())){
            shoppingCar.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
         }
        shoppingCar.setPage(new PageDomain(request, response));
        return R.data(shoppingCarService.findPage(shoppingCar));
    }

    /**
     * 获取购物车详细信息
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:query')")
    @GetMapping(value = "/{id}")
    public R<ShoppingCar> detail(@PathVariable("id") String id)
    {
        return R.data(shoppingCarService.get(id));
    }

    /**
     * 新增购物车
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:add')")
    @Log(title = "购物车", businessType = BusinessType.INSERT)
    @PostMapping
    public R add(@RequestBody @Validated  ShoppingCar shoppingCar)
    {
        return R.status(shoppingCarService.save(shoppingCar));
    }

    /**
     * 修改购物车
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:edit')")
    @Log(title = "购物车", businessType = BusinessType.UPDATE)
    @PutMapping
    public R edit(@RequestBody @Validated ShoppingCar shoppingCar)
    {
        return R.status(shoppingCarService.save(shoppingCar));
    }


    /**
     * 删除购物车
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:remove')")
    @Log(title = "购物车", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public R remove(@PathVariable String[] ids)
    {
        return R.status(shoppingCarService.deleteShoppingCarByIds(ids));
    }

    /**
     * 获取最大编号
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:query')")
    @GetMapping("/findMaxSort")
    public R findMaxSort()
    {
        return R.data(shoppingCarService.findMaxSort(new ShoppingCar()));
    }

    /**
     * 导出购物车列表
     */
    @PreAuthorize("@ss.hasPermi('biz:bizShoppingCar:export')")
    @Log(title = "购物车", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public R export(ShoppingCar shoppingCar)
    {

            if(SecurityUtils.isAdmin(SecurityUtils.getUserId())){
                    shoppingCar.setCompanyId(null);
            }else{
                         shoppingCar.setCompanyId(SecurityUtils.getLoginUser().getUser().getCompanyId());
                     }
        List<ShoppingCar> list = shoppingCarService.findList(shoppingCar);
        ExcelUtil<ShoppingCar> util = new ExcelUtil<ShoppingCar>(ShoppingCar.class);
        return util.exportExcel(list, "购物车数据");
    }

}
